import authDialogSendData from './auth'              //弹窗组件
import menuDialogForm from './menu'              //弹窗组件
import agentHttp from '@/utils/agentHttp'    //请求方法
import {confirm} from '@/utils/confirm'      //二次确认组件
export default {
  name: 'menu',
  created(){
      this.selectMenuListData();
  },
  components:{menuDialogForm,authDialogSendData},
  data() {
  	return {
      searchForm:{},
      //弹窗状态数据
      authDialogSendData:{
        isAdd:"",
        dialogFormVisible:"",
        id:"",
        oneLevel:true,
      }, 
      permission:[],
      permissionRole:[], 
      pro_role_name:"",
      menuDialogSendData:{ 
        data:{menuName:""},
        show:false,
        dialogFormVisible:"",
        id:""
      },
      listMenu:[],
      systemPermissionEnum:{},
      /*****权限参数****/
      checkAll: true,
      isIndeterminate: true,
      permissionList:[],
      permissionArray:[],
      permissionLength:null,
      /****************/
      /*****角色参数****/
      roleList:[],
      roleId:"",
      /****************/
      defaultProps: {
        children: 'childrenMenuList',
        label: 'menuName'
      },
      formLabelWidth:'170px',
      //弹窗或者查看页面信息
      dialogForm:{
        type:[]
      },
      isSelected:false,
      currentRoleData :{}
    }
  },
  watch:{ 
      roleId: function (newQuestion, oldQuestion) { 
        this.isSelected = true; 
        this.currentRoleData = newQuestion;
        this.permissionArray = []; 
        this.queryPermission();   
        //绑定已有权限
        for (var i = 0; i < newQuestion.rolePermissionList.length; i++) { 
            //只显示当前菜单
            if(this.menuDialogSendData.data.id  === newQuestion.rolePermissionList[i].menuId){
                  this.bindPermissionArray(newQuestion.rolePermissionList[i].id);
              
            } 
        } 
      } 
  },
  methods: { 
    //分配权限
    editData(){
      var parameter = {
          permissionArrays : this.permissionArray, 
          roleId : this.currentRoleData.id,
          menuId : this.menuDialogSendData.data.id
      };  
      agentHttp.postJson('adminApi/admin/system_menu/distribution_role',parameter).then((data)=>{  
            if(data.success){   
                this.$message.success(data.data);
                //this.queryRole(); 
            } 
      }) 
      
    },
    //新增、编辑
    handleEdit(store, data , isOneLevel){ 
      window.event? window.event.cancelBubble = true : e.stopPropagation();
      this.authDialogSendData.isAdd=false;
      this.authDialogSendData.store=store;
      this.authDialogSendData.data=data;
      this.authDialogSendData.dialogFormVisible=true;
      this.authDialogSendData.oneLevel=isOneLevel;
    },
    
    append(store, data) { 
      window.event? window.event.cancelBubble = true : e.stopPropagation();
      this.authDialogSendData.isAdd=true;
      this.authDialogSendData.store=store;
      this.authDialogSendData.data=data;
      this.authDialogSendData.dialogFormVisible=true;
      this.authDialogSendData.oneLevel=false;
      
      //store.append({ id: this.id++, label: 'testtest', children: [] }, data);
    },
    //删除菜单
    remove(store, data) { 
      window.event? window.event.cancelBubble = true : e.stopPropagation();
      confirm(()=>{
          agentHttp.del('adminApi/admin/system_menu/delete/'+data.id).then((data)=>{
            if(data.success){  
                this.$message.success(data.data);
                this.selectMenuListData();
                //这里编写数据赋值或者其他逻辑  例如：this.tableData = data;
            } 
          })
        },()=>{
           this.$message.info('已取消删除'); 
        },"删除菜单会删除对应的子菜单,以及菜单对应的权限,您确定这样做吗?")
      //store.remove(data);
    },

    addTopLevel(store, data){ 
      window.event? window.event.cancelBubble = true : e.stopPropagation();
      this.authDialogSendData.isAdd=true;
      this.authDialogSendData.store=store;
      this.authDialogSendData.data=data;
      this.authDialogSendData.dialogFormVisible=true;
      this.authDialogSendData.oneLevel=true;
    },
    nodeClick(data, checked, indeterminate){ 
      if(data.parentMenuId===0){
        return false;
      } 
      this.menuDialogSendData.show=true;
      this.menuDialogSendData.data=data;
      this.menuDialogSendData.checked=checked;
      this.menuDialogSendData.indeterminate=indeterminate;
      this.isSelected = false;
      //清空权限列表数据
      this.permissionList = [];
      //this.queryPermission(data.id);
      this.queryRole();

      
    },
    renderContent(h, { node, data, store }) {
      if(data.parentMenuId===0){//parentMenuId
        return (
          <span>
            <span >
              <span>{node.label}</span>
              <span class="element">
                <el-button size="mini" on-click={ () => this.append(store, data) }><i class="el-icon-plus"></i></el-button>
                <el-button size="mini" on-click={ () => this.handleEdit(store, data ,true) }><i class="el-icon-edit"></i></el-button>
                <el-button size="mini" on-click={ () => this.remove(store, data) }><i class="el-icon-delete"></i></el-button>
              </span>
            </span>
            
          </span>);
      }else{
         return (
          <span>
            <span >
              <span>{node.label}</span>
              <span class="element">
                 <el-button size="mini" on-click={ () => this.handleEdit(store, data ,false) }><i class="el-icon-edit"></i></el-button>
                 <el-button size="mini" on-click={ () => this.remove(store, data) }><i class="el-icon-delete"></i></el-button>
              </span>
            </span>
          </span>);
      }
    },
    //菜单
    selectMenuListData(){  
        agentHttp.get('adminApi/admin/system_menu/query_menu_list').then((data)=>{  
              if(data.success){   
                this.listMenu =  data.data.listMenu;    
                this.systemPermissionEnum = data.data.systemPermissionEnum;
              } 
        }) 
    },
    //权限
    queryPermission(){    
        agentHttp.get('adminApi/admin/system_menu/query_menu/and_permission/'+this.menuDialogSendData.data.id).then((data)=>{  
              if(data.success){  
                 this.permissionLength = 0;
                 this.permissionList = data.data; 
                 for (var i = 0; i < this.permissionList.length; i++) { 
                   this.permissionLength += this.permissionList[i].childrenPermissionList.length 
                 }  
              } 
        }) 
    },
    //角色
    queryRole(){
      agentHttp.get('adminApi/admin/system_menu/query_menu/role_and_permission').then((data)=>{  
              if(data.success){     
                  this.roleList = data.data; 
              } 
      }) 
      
    },
    //全选反选
    handleCheckAllChange(event) {   
        if(event.target.checked){
          for (var i = 0; i < this.permissionList.length; i++) { 
              var data = this.permissionList[i].childrenPermissionList;
              for (var j = 0; j < data.length; j++) {
                 var permission = data[j];
                // this.permissionArray.push(permission.id);
                 this.bindPermissionArray(permission.id);
              } 
          } 
        }
        else {
             this.permissionArray = [];
        } 
        this.isIndeterminate = false;
    },
    handleCheckedCitiesChange(value) {  
        let checkedCount = value.length;
        this.checkAll = checkedCount === this.permissionLength;
        this.isIndeterminate = checkedCount > 0 && checkedCount < this.permissionLength;
    },
    bindPermissionArray( id ){
        var flag = false;
        if(this.permissionArray.length > 0){
            for (var i = 0; i < this.permissionArray.length; i++) {
                if(this.permissionArray[i] != id ){
                  flag = true; 
                }else {
                  flag = false;
                  break;
                }
            }  
            if(flag){
               this.permissionArray.push(id);
            }
        }
        else{
            this.permissionArray.push(id);
        }
    }
  } 
  
    
}